﻿using System;
using System.Collections.Generic;
using SystemModule;

namespace M2Server
{
    public partial class TPlayObject
    {

        /// <summary>
        /// 获取商铺分页数量
        /// </summary>
        /// <param name="nItemListCount"></param>
        /// <returns></returns>
        public int ClientGetShopItemList_GetPageCount(int nItemListCount)
        {
            int result = 0;
            if (nItemListCount > 0)
            {
                result = nItemListCount / 10;
                if ((nItemListCount % 10) > 0)
                {
                    result++;
                }
            }
            return result;
        }

        /// <summary>
        /// 客户端取商铺物品列表
        /// </summary>
        private void ClientGetShopItemList(int nPage, int nType)
        {
            int nPageCount;
            int n01;
            int n02;
            int n03;
            string sSendStr;
            string sSendStr1;
            TShopItem pShopInfo;
            TShopItem ShopInfo;
            IList<TShopItem> TempList;
            if (m_boCanQueryShopItme)// 是否可以刷新商铺
            {
                return;
            }
            m_boCanQueryShopItme = true; // 是否可以刷新商铺
            try
            {
                try
                {
                    if (M2Share.g_ShopItemList == null)
                    {
                        return;
                    }
                    n01 = 0;
                    n02 = 0;
                    n03 = 0;
                    sSendStr = "";
                    sSendStr1 = "";
                    if (M2Share.g_ShopItemList.Count > 0)
                    {
                        for (int i = 0; i < M2Share.g_ShopItemList.Count; i++)
                        {
                            pShopInfo = M2Share.g_ShopItemList[i];
                            ShopInfo = pShopInfo;
                            if (Convert.ToInt32(ShopInfo.ShopClass) == nType)
                            {
                                n03++;
                            }
                        }
                    }
                    nPageCount = ClientGetShopItemList_GetPageCount(n03);
                    if (nPage > 0)
                    {
                        if (M2Share.g_ShopItemList.Count >= nPage * 10)
                        {
                            TempList = new List<TShopItem>();
                            try
                            {
                                for (int i = 0; i < M2Share.g_ShopItemList.Count; i++)
                                {
                                    pShopInfo = M2Share.g_ShopItemList[i];
                                    ShopInfo = pShopInfo;
                                    if (Convert.ToInt32(ShopInfo.ShopClass) == nType)
                                    {
                                        TempList.Add(pShopInfo);
                                    }
                                }
                                for (int i = nPage * 10; i < TempList.Count; i++)
                                {
                                    pShopInfo = TempList[i];
                                    ShopInfo = pShopInfo;
                                    n01++;
                                    sSendStr = sSendStr + EncryptUnit.EncodeBuffer<TShopItem>(ShopInfo) + "/";
                                    if (n01 >= 10)
                                    {
                                        break;
                                    }
                                }
                            }
                            finally
                            {
                                Dispose(TempList);
                            }
                        }
                    }
                    else
                    {
                        if (M2Share.g_ShopItemList.Count > 0)
                        {
                            for (int i = 0; i < M2Share.g_ShopItemList.Count; i++)
                            {
                                pShopInfo = M2Share.g_ShopItemList[i];
                                ShopInfo = pShopInfo;
                                if (Convert.ToInt32(ShopInfo.ShopClass) == nType)
                                {
                                    n01++;
                                    sSendStr = sSendStr + EncryptUnit.EncodeBuffer<TShopItem>(ShopInfo) + "/";
                                    if (n01 >= 10)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if (sSendStr != "")
                    {
                        this.SendMsg(this, Grobal2.RM_OPENSHOP, 0, nPageCount, nPage + 1, n01, sSendStr);// 齐珍类型
                    }
                    if (M2Share.g_ShopItemList.Count > 0)
                    {
                        for (int i = 0; i < M2Share.g_ShopItemList.Count; i++)
                        {
                            pShopInfo = M2Share.g_ShopItemList[i];
                            ShopInfo = pShopInfo;
                            if (Convert.ToInt32(ShopInfo.ShopClass) == 5)
                            {
                                n02++;
                                sSendStr1 = sSendStr1 + EncryptUnit.EncodeBuffer<TShopItem>(ShopInfo) + "/";
                                if (n02 >= 5)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    if (sSendStr1 != "")
                    {
                        this.SendMsg(this, Grobal2.RM_OPENSHOPSpecially, 0, nPageCount, nPage + 1, n02, sSendStr1);
                    }
                }
                catch
                {
                    M2Share.MainOutMessage("[异常] TPlayObject.ClientGetShopItemList");
                }
            }
            finally
            {
                m_boCanQueryShopItme = false;// 是否可以刷新商铺
            }
        }

        /// <summary>
        /// 客户端购买商铺物品
        /// </summary>
        /// <param name="sItemName"></param>
        private void ClientBuyShopItem(string sItemName)
        {
            TStdItem StdItem;
            TUserItem UserItem = null;
            int nPrice;
            int nCount;
            bool boBuyShopItem;
            if (m_boBuyShopItem) // 正在购买商铺物品中
            {
                return;
            }
            m_boBuyShopItem = true;
            boBuyShopItem = false;
            try
            {
                try
                {
                    StdItem = M2Share.UserEngine.GetShopStdItem(sItemName);
                    if (StdItem != null)
                    {
                        nPrice = StdItem.Price / 100;
                        nCount = StdItem.NeedLevel;// 一次可以购买的数量
                        if (nCount <= 0)
                        {
                            nCount = 1;// 防止死循环
                        }
                        if ((m_nGameGold >= nPrice) && (nPrice >= 0) && (nCount > 0))
                        {
                            for (int i = 0; i < nCount; i++)
                            {
                                if (this.m_ItemList.Count >= Grobal2.MAXBAGITEM)
                                {
                                    break;
                                }
                                if (IsEnoughBag())
                                {
                                    UserItem = new TUserItem();
                                    if (M2Share.UserEngine.CopyToUserItemFromName(sItemName, UserItem))
                                    {
                                        if ((StdItem.StdMode == 60) && (StdItem.Shape != 0) && (StdItem.AniCount == 2))
                                        {
                                            if (UserItem.btValue[0] < 5)// 购买药酒时，品质低于5时，重新计算
                                            {
                                                UserItem.btValue[0] = Convert.ToByte(HUtil32.Random(3) + 5);
                                            }
                                        }
                                        if (this.AddItemToBag(UserItem))
                                        {
                                            if (StdItem.NeedIdentify == 1)
                                            {
                                                M2Share.AddGameDataLog("30" + "\09" + this.m_sMapName + "\09" + (this.m_nCurrX).ToString() + "\09" + (this.m_nCurrY).ToString()
                                                    + "\09" + this.m_sCharName + "\09" + StdItem.Name + "\09" + (UserItem.MakeIndex).ToString() + "\09" + "(" + (HUtil32.LoWord(StdItem.DC)).ToString()
                                                    + "/" + (HUtil32.HiWord(StdItem.DC)).ToString() + ")" + "(" + (HUtil32.LoWord(StdItem.MC)).ToString() + "/" + (HUtil32.HiWord(StdItem.MC)).ToString()
                                                    + ")" + "(" + (HUtil32.LoWord(StdItem.SC)).ToString() + "/" + (HUtil32.HiWord(StdItem.SC)).ToString() + ")" + "(" + (HUtil32.LoWord(StdItem.AC)).ToString()
                                                    + "/" + (HUtil32.HiWord(StdItem.AC)).ToString() + ")" + "(" + (HUtil32.LoWord(StdItem.MAC)).ToString() + "/" + (HUtil32.HiWord(StdItem.MAC)).ToString() + ")"
                                                    + (UserItem.btValue[0]).ToString() + "/" + (UserItem.btValue[1]).ToString() + "/" + (UserItem.btValue[2]).ToString() + "/" + (UserItem.btValue[3]).ToString()
                                                    + "/" + (UserItem.btValue[4]).ToString() + "/" + (UserItem.btValue[5]).ToString() + "/" + (UserItem.btValue[6]).ToString() + "/" + (UserItem.btValue[7]).ToString()
                                                    + "/" + (UserItem.btValue[8]).ToString() + "/" + (UserItem.btValue[14]).ToString() + "\09" + (UserItem.Dura).ToString() + "/" + (UserItem.DuraMax).ToString());
                                            }
                                            SendAddItem(UserItem);
                                            boBuyShopItem = true;
                                            this.SysMsg("[成功] " + sItemName + " 购买成功！", TMsgColor.c_Blue, TMsgType.t_Hint);
                                        }
                                    }
                                    else
                                    {
                                       UserItem=null;
                                    }
                                }
                                else
                                {
                                    this.SendMsg(this, Grobal2.RM_BUYSHOPITEM_FAIL, 0, 2, 0, 0, sItemName);// 包裹满
                                    break;
                                }
                            }
                        }
                        else
                        {
                            this.SendMsg(this, Grobal2.RM_BUYSHOPITEM_FAIL, 0, 1, 0, 0, sItemName);
                        }
                        if (boBuyShopItem)// 购买成功
                        {
                            DecGameGold(nPrice);
                            this.GameGoldChanged();
                            m_BuyShopPrice = nPrice;// 每次商铺花的元宝数量，$BuyShop变量使用
                            if (M2Share.g_FunctionNPC != null)// 商铺购买触发
                            {
                                M2Share.g_FunctionNPC.GotoLable(this, "@BuyShop", false);
                            }
                        }
                    }
                    else
                    {
                        this.SendMsg(this, Grobal2.RM_BUYSHOPITEM_FAIL, 0, 3, 0, 0, sItemName);
                    }
                }
                catch
                {
                    M2Share.MainOutMessage("[异常] TPlayObject.ClientBuyShopItem");
                }
            }
            finally
            {
                m_boBuyShopItem = false;
            }
        }

        /// <summary>
        /// 客户端商铺赠送
        /// </summary>
        /// <param name="sMsg"></param>
        private unsafe void ClientBuyShopItemGive(string sMsg)
        {
            string GiveUseName = null;
            string GiveItemName = null;
            string GiveMyName = null;
            TStdItem StdItem = null;
            TUserItem UserItem = null;
            int nPrice;
            int nCount;
            TPlayObject PlayObjectGive;
            bool boBuyShopItem;
            if (m_boBuyShopItem)// 正在购买商铺物品中
            {
                return;
            }
            m_boBuyShopItem = true;
            boBuyShopItem = false;
            try
            {
                try
                {
                    sMsg = HUtil32.GetValidStr3(sMsg, ref GiveItemName, new string[] { "/" });
                    sMsg = HUtil32.GetValidStr3(sMsg, ref GiveUseName, new string[] { "/" });
                    sMsg = HUtil32.GetValidStr3(sMsg, ref GiveMyName, new string[] { "/" });
                    GiveUseName = EncryptUnit.DeCodeString(GiveUseName);
                    GiveItemName = EncryptUnit.DeCodeString(GiveItemName);
                    GiveMyName = EncryptUnit.DeCodeString(GiveMyName);
                    StdItem = M2Share.UserEngine.GetShopStdItem(GiveItemName);
                    if (StdItem != null)
                    {
                        PlayObjectGive = M2Share.UserEngine.GetPlayObject(GiveUseName);
                        if (PlayObjectGive == null) // 角色没在线
                        {
                            this.SendMsg(this, Grobal2.RM_BUYSHOPITEMGIVE_FAIL, 0, 4, 0, 0, GiveUseName);// 赠送的人没在线
                            return;
                        }
                        nPrice = StdItem.Price / 100;
                        nCount = StdItem.NeedLevel;// 一次可以购买的数量 20090114
                        if (nCount <= 0)
                        {
                            nCount = 1; // 防止死循环
                        }
                        if ((m_nGameGold >= nPrice) && (nPrice >= 0) && (nCount > 0))
                        {
                            for (int i = 0; i < nCount; i++)
                            {
                                if (PlayObjectGive.m_ItemList.Count >= Grobal2.MAXBAGITEM)
                                {
                                    break;
                                }
                                if (PlayObjectGive.IsEnoughBag()) // 人物背包
                                {
                                    UserItem = new TUserItem();
                                    if (M2Share.UserEngine.CopyToUserItemFromName(GiveItemName, UserItem))
                                    {
                                        if (PlayObjectGive.AddItemToBag(UserItem))
                                        {
                                            if (StdItem.NeedIdentify == 1)// 攻 魔法 道术 防御 魔御
                                            {
                                                M2Share.AddGameDataLog("30" + "\09" + this.m_sMapName + "\09" + (this.m_nCurrX).ToString() + "\09" +
                                                    (this.m_nCurrY).ToString() + "\09" + PlayObjectGive.m_sCharName + "\09" + StdItem.Name + "\09" +
                                                    (UserItem.MakeIndex).ToString() + "\09" + "(" + (HUtil32.LoWord(StdItem.DC)).ToString() + "/" +
                                                    (HUtil32.HiWord(StdItem.DC)).ToString() + ")" + "(" + (HUtil32.LoWord(StdItem.MC)).ToString() + "/" + (HUtil32.HiWord(StdItem.MC)).ToString()
                                                    + ")" + "(" + (HUtil32.LoWord(StdItem.SC)).ToString() + "/" + (HUtil32.HiWord(StdItem.SC)).ToString() + ")" + "(" +
                                                    (HUtil32.LoWord(StdItem.AC)).ToString() + "/" + (HUtil32.HiWord(StdItem.AC)).ToString() + ")" + "(" + (HUtil32.LoWord(StdItem.MAC)).ToString()
                                                    + "/" + (HUtil32.HiWord(StdItem.MAC)).ToString() + ")" + (UserItem.btValue[0]).ToString() + "/" +
                                                    (UserItem.btValue[1]).ToString() + "/" + (UserItem.btValue[2]).ToString() + "/" + (UserItem.btValue[3]).ToString()
                                                    + "/" + (UserItem.btValue[4]).ToString() + "/" + (UserItem.btValue[5]).ToString() + "/" +
                                                    (UserItem.btValue[6]).ToString() + "/" + (UserItem.btValue[7]).ToString() + "/" + (UserItem.btValue[8]).ToString()
                                                    + "/" + (UserItem.btValue[14]).ToString() + "\09" + this.m_sCharName + "(赠)");
                                            }
                                            PlayObjectGive.SendAddItem(UserItem);
                                            boBuyShopItem = true;
                                            this.SendMsg(this, Grobal2.RM_BUYSHOPITEMGIVE_SUCCESS, 0, 1, 0, 0, "[成功]赠送给 " + GiveItemName + " 物品 " + GiveUseName + " 成功！");
                                            PlayObjectGive.SysMsg("[提示]你的朋友 " + GiveMyName + " 在商铺里给你买了 " + GiveItemName + " 请您查收！", TMsgColor.c_Blue, TMsgType.t_Hint);
                                        }
                                    }
                                    else
                                    {
                                       UserItem=null;
                                    }
                                }
                                else
                                {
                                    this.SendMsg(this, Grobal2.RM_BUYSHOPITEMGIVE_FAIL, 0, 3, 0, 0, GiveItemName);// 背包满
                                    break;
                                }
                            }
                        }
                        else
                        {
                            this.SendMsg(this, Grobal2.RM_BUYSHOPITEMGIVE_FAIL, 0, 1, 0, 0, GiveItemName); // 元宝不够
                        }
                        if (boBuyShopItem)// 购买成功
                        {
                            DecGameGold(nPrice);
                            this.GameGoldChanged();
                            m_BuyShopPrice = nPrice;// 每次商铺花的元宝数量，$BuyShop变量使用 20090106
                            if (M2Share.g_FunctionNPC != null)
                            {
                                // 商铺购买触发
                                M2Share.g_FunctionNPC.GotoLable(this, "@BuyShop", false);
                            }
                        }
                    }
                    else
                    {
                        // 没有找到这个物品
                        this.SendMsg(this, Grobal2.RM_BUYSHOPITEMGIVE_FAIL, 0, 2, 0, 0, GiveItemName);
                    }
                }
                catch
                {
                    M2Share.MainOutMessage("[异常] TPlayObject.ClientBuyShopItemGive");
                }
            }
            finally
            {
                m_boBuyShopItem = false;
            }
        }

        /// <summary>
        /// 客户端商铺兑换灵符
        /// </summary>
        /// <param name="nGameGird"></param>
        private unsafe void ClientExchangeGameGird(int nGameGird)
        {
            try
            {
                if (!M2Share.g_Config.g_boGameGird)
                {
                    this.SendMsg(this, Grobal2.RM_EXCHANGEGAMEGIRD_FAIL, 0, 0, 0, 0, "[失败]对不起，本服务没有开放");
                    return;
                }
                if (m_nGameGold <= 0)
                {
                    this.SendMsg(this, Grobal2.RM_EXCHANGEGAMEGIRD_FAIL, 0, 0, 0, 0, "[失败]您的" + M2Share.g_Config.sGameGoldName + "不够，请冲值！ 本服"
                        + (M2Share.g_Config.g_nGameGold).ToString() + "个" + M2Share.g_Config.sGameGoldName + "兑换1个灵符！");
                    return;
                }
                if (m_nGameGold >= nGameGird * M2Share.g_Config.g_nGameGold)// 当前元宝>兑换数量*每个灵符单价
                {
                    DecGameGold(nGameGird * M2Share.g_Config.g_nGameGold);// 给角色减元宝
                    IncGameGird(nGameGird);
                    this.GameGoldChanged();
                    this.SendMsg(this, Grobal2.RM_EXCHANGEGAMEGIRD_SUCCESS, 0, 0, 0, 0, "[成功]您成功的兑换了 " + (nGameGird).ToString() + " 个灵符");
                }
                else
                {
                    this.SendMsg(this, Grobal2.RM_EXCHANGEGAMEGIRD_FAIL, 0, 0, 0, 0, "[失败]您的" + M2Share.g_Config.sGameGoldName + "不够，请冲值！ 本服"
                        + (M2Share.g_Config.g_nGameGold).ToString() + "个" + M2Share.g_Config.sGameGoldName + "兑换1个灵符！");// 元宝不足
                }
            }
            catch
            {
                M2Share.MainOutMessage("[异常] TPlayObject.ClientExchangeGameGird");
            }
        }
    }
}
